//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.huitu.whht.api.core.common.response;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.huitu.api.exception.InvalidKeyException;
import com.huitu.api.rest.*;
import com.huitu.whht.api.core.common.base.DataUtils;

import java.util.List;
import java.util.Map;

public final class ApiResultBuilder {
    private ApiResultBuilder() {
    }

    public static ApiResult create(ApiResource resource, Object result) {
        return new SuccessResult(resource, result);
    }

    public static ApiResult create(ApiResource resource, Object result, PagingMap pagingMap) {
        return (ApiResult) (pagingMap != null ? new PagingResult(resource, result, pagingMap.getPaging()) : new SuccessResult(resource, result));
    }

    public static ApiResult create(ApiResource resource, Object result, List list) {
        return (ApiResult) (list instanceof Page ? new PagingResult(resource, result, getPagingMap(list).getPaging()) : new SuccessResult(resource, result));
    }

    public static ApiResult create(ApiResource resource, List result) {
        return (ApiResult) (result instanceof Page ? new PagingResult(resource, result, getPagingMap(result).getPaging()) : new SuccessResult(resource, result));
    }

    public static ApiResult create(ApiResource resource, Map<String, Object> result) {
        if (result instanceof PagingMap) {
            PagingMap map = (PagingMap) result;
            return new PagingResult(resource, result, map.getPaging());
        } else {
            return new SuccessResult(resource, result);
        }
    }

    public static ApiResult create(ApiResource resource, Map<String, Object> result, PagingMap pagingMap) {
        return (ApiResult) (pagingMap != null ? new PagingResult(resource, result, pagingMap.getPaging()) : new SuccessResult(resource, result));
    }

    public static ApiResult create(ApiResource resource, List<Map<String, Object>> list, String key) throws InvalidKeyException {
        if (list != null && !((Map) list.get(0)).containsKey(key)) {
            throw new InvalidKeyException(String.format("%s does not exists in list.", key));
        } else {
            Map<String, Object> result = DataUtils.list2Map(list, key);
            return (ApiResult) (list instanceof Page ? new PagingResult(resource, result, getPagingMap(list).getPaging()) : create(resource, result));
        }
    }

    private static PagingMap getPagingMap(List list) {
        if (list instanceof Page) {
            PageInfo pageInfo = new PageInfo(list);
            Paging paging = new Paging();
            paging.setCount(pageInfo.getPages());
            paging.setTotal((int) pageInfo.getTotal());
            paging.setIndex(pageInfo.getPageNum());
            paging.setFinale(pageInfo.isIsLastPage());
            PagingMap pagingMap = new PagingLinkedHashMap(paging, list.size());
            return pagingMap;
        } else {
            return null;
        }
    }
}
